package part18;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * @author wcj
 * @Date 2021/6/2 15:41
 * @Version 1.0
 */
public class Part182 {

    /**
     * 匿名内部类的形式实现文件过滤器：这样的好处是代码更加简短，不需要另外再写一个类
     *
     * @param regex
     * @return
     */
    public static FilenameFilter filter(final String regex) {
        return new FilenameFilter() {
            private Pattern pattern = Pattern.compile(regex);

            @Override
            public boolean accept(File dir, String name) {
                return pattern.matcher(name).matches();
            }
        };
    }

    public static void main(String[] args) {
        File file = new File("./src");
        String[] list;
        if (args.length == 0) {
            list = file.list();
        } else {
            list = file.list(filter(args[0]));
        }
        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
        for (String s : list) {
            System.out.println(s);
        }
    }
}
